<template>
  <el-dialog :visible.sync="visible" v-if="visible" width="50%" title="师资新增" top="20px" :append-to-body="appendToBody">
<el-card>
  <!--工具栏-->
<div class="head-container">
          <div v-if="crud.props.searchToggle">
            <!-- 搜索 -->
            <el-input v-model="query.keyword" clearable size="small" placeholder="请输入大学名称" style="width: 200px;"
              class="filter-item" @keyup.enter.native="crud.toQuery" />
            <el-select clearable class="filter-item" :remote="true" v-model="query.eduType" @change="crud.toQuery" filterable placeholder="请选择学历类型">
                <el-option
                  v-for="item in eduTypes"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              
    <el-cascader
      :options="regionData"
      :props="{children:'child'}"
      class="filter-item"
      @change="cityChange"
      v-model="selectedOptions">
    </el-cascader>
            <rrOperation />
            
          </div>
          <crudOperation show="" :permission="permission" />
        </div>
</el-card>

        <el-card style="margin-top: 20px;">
          <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="handleSelectionChange">
    <el-table-column width="55" type="selection"/>
          <el-table-column  label="校徽">
            <template slot-scope="scope">
              <el-image lazy :src="scope.row.emblem" class="n_avatar" :previewSrcList="[scope.row.emblem]"/>
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="uniName" label="名称" />
          <el-table-column prop="describeInfo" label="地区" />
          <el-table-column label="地区">
            <template slot-scope="scope">
              <span v-if="scope.row.district">
                {{scope.row.province}}/{{scope.row.city}}/{{scope.row.district}}
              </span>
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="address" label="地址" />
        </el-table>
        </el-card>
        <!--分页组件-->
        <pagination />
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="confirm(true)" :loading="submitLoading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import crudSchool from '@/api/web/school'
  import CRUD, {
    presenter,
    header,
    form,
    crud
  } from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'
  import pagination from '@crud/Pagination'
  import DateRangePicker from '@/components/DateRangePicker'
  import regionData from '@/utils/area';
  export default {
    name: 'User',
    props:{
      appendToBody:{
        type:Boolean,
        default:false,
      }
    },
    components: {
      crudOperation,
      rrOperation,
      udOperation,
      pagination,
      DateRangePicker,
    },
    cruds() {
      return CRUD({
        title: '用户',
        url: 'api/uni/list',
        methodStr: 'post',
        dataStr: ['data', 'uniList'],
        optShow: {
          reset: false,
          // othoer: true,
        },
        crudMethod: { ...crudSchool}
      })
    },
    mixins: [presenter(), header(), crud()],
    data() {
      // 自定义验证
      const validPhone = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入电话号码'))
        } else if (!isvalidPhone(value)) {
          callback(new Error('请输入正确的11位手机号码'))
        } else {
          callback()
        }
      }
      return {
        height: document.documentElement.clientHeight - 180 + 'px;',
        visible:false,
        submitLoading:false,
        regionData,
        schoolSelect:'',
        selectUniObj:{},
        selectedOptions:[],
        selectedRows:[],
        eduTypes:[
          {label:'专科',value:'1'},
          {label:'本科',value:'2'},

        ],
        defaultProps: {
          children: 'children',
          label: 'name',
          isLeaf: 'leaf'
        },
        permission: {
          add: ['admin', 'user:add'],
          edit: ['admin', 'article:edit'],
          del: ['admin', 'user:del']
        },
      }
    },
    mounted: function() {
      const that = this
      window.onresize = function temp() {
        that.height = document.documentElement.clientHeight - 180 + 'px;'
      }
    },
    methods: {
    add() {
      this.visible = true
      this.formData = {}
      this.action = 'add'
    },
    edit(record) {
      this.action = 'edit'
      this.visible = true
      this.formData = record
    },
      cityChange(){
        
        if (this.selectedOptions) {
          this.$set(this.query,'districtCode',this.selectedOptions[this.selectedOptions.length - 1])
        }else{
          this.$set(this.query,'districtCode','')
        }
        this.crud.toQuery()
      },
      del(item){
        this.$confirm(`文章删除后不可恢复，确认删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$set(item,'del_loading',true)
          crudArticle.del(item.id).then(res=>{
            this.$set(item,'del_loading',false)
            if(res.code == 200){
              this.$message({
                type: 'success',
                message: res.message
              })
              this.crud.toQuery()
            }else{
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        })

      },
      changeStatus(item,type,loadingStr) {
        let confirmContent = ''
        let confirmStatus = false
        if(type == 1){
          if(item.ifRecommend){
            confirmContent = '取消推荐'
          }else{
            confirmContent = '推荐'
          }
          confirmStatus = !item.ifRecommend
        }else{
          if(item.ifSelected){
            confirmContent = '取消精选'
          }else{
            confirmContent = '精选'
          }
          confirmStatus = !item.ifSelected
        }
        this.$confirm(`是否${confirmContent}该文章`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$set(item,loadingStr,true)
          crudArticle.changeStatus({id:item.id,type:type,status:confirmStatus}).then(res=>{
            this.$set(item,loadingStr,false)
            if(res.code == 200){
              this.$message({
                type: 'success',
                message: res.message
              })
              this.crud.toQuery()
            }else{
              this.$message({
                type: 'arror',
                message: res.message
              })
            }
          }).catch(e=>{
            this.$set(item,loadingStr,false)
          })
        })

      },
      handleSelectionChange(rows) {
        this.selectedRows = rows
      },
      confirm(closeDialog){
        if(closeDialog){
          if(this.selectedRows.length){
            this.$emit('ok',this.selectedRows)
            this.visible = false
          }else{
            this.$message.error('请选择一所大学')
          }
        }
      },
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .vue-treeselect__control,
  ::v-deep .vue-treeselect__placeholder,
  ::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }

  .n_avatar {
    max-width: 120px;
    max-height: 120px;
  }
  .btn{
    margin: 0;
  }
</style>
  